import React, { Component} from 'react';
import { connect } from 'dva';
import { message, Modal, Pagination, Popconfirm, Button } from 'antd';
import styles from './../Users.css';
import moment from 'moment';
import Factory from '../../factory';
import numeral from 'numeral';
import * as datetime from '../../../utils/datetime'
import dataEditCreate from '../../addons/DataEdit'
import dataTableCreate from '../../addons/DataTable'
import * as compDiviService from '../../../services/AccountDivi';
import * as menuService from '../../../services/menus';
import * as common from '../../../utils/common';

const daoName = "COMP_LOAN";
const ButtonGroup = Button.Group;
const renderNumber = (num) => {
  let value = ''
  if (num) {
    value = numeral(num).format('0,0')
  }
  return value
};

const columns = [
  { title: '客户名称', width: 250, sorter: true, nowrap: true,caption:'KHMC', key: 'KHMC', dispaly: true, filter: true },
  { title: '帐户', width: 210, sorter: true, dataIndex: 'ZH', key: 'ZH', dispaly: true, filter: true },
  { title: '开户网点', width: 140, sorter: true, dataIndex: 'KHWD', key: 'KHWD', dispaly: true, filter: true },
  { title: '开户日期', width: 110, sorter: true, key: 'KHRQ', caption: "KHRQ", dispaly: true, filter: true,render: data => datetime.format(data.KHRQ, "yyyy-MM-dd") },
  { title: '起息日', width: 100, sorter: true, key: 'QXR',caption: "QXR", dispaly: true, filter: true ,render: data => datetime.format(data.QXR, "yyyy-MM-dd")},
  { title: '到期日', width: 100, sorter: true, key: 'DQR',caption: "DQR", dispaly: true, filter: true ,render: data => datetime.format(data.DQR, "yyyy-MM-dd")},
  { title: '企业规模(国标)', width: 150, sorter: true, dataIndex: 'QYGM', key: 'QYGM', dispaly: true, filter: true },
  { title: '科目', width: 180, sorter: true, nowrap: true,caption:'KM', key: 'KM', dispaly: true, filter: true },
  { title: '利率', width: 110, sorter: true, dataIndex: 'INRT', key: 'INRT', dispaly: true, filter: true },
  { title: '当前余额', width: 110, sorter: true, dataIndex: 'DQYE', key: 'DQYE', dispaly: true, filter: true },
  { title: '币种', width: 100, sorter: true, dataIndex: 'BZ', key: 'BZ', dispaly: true, filter: true },
  { title: '开户日均', width: 110, sorter: true, dataIndex: 'KHRJ', key: 'KHRJ', dispaly: true, filter: true },
  { title: '客户经理1', width: 180, sorter: false, dataIndex: 'YG1', key: 'YG1', dispaly: true, filter: false },
  { title: '分配比例1(%)', width: 100, sorter: false, className: "column-money", caption:'ZB1',render: data => renderNumber(data.ZB1), key: 'ZB1', dispaly: true, filter: false },
  { title: '客户经理2', width: 180, sorter: false, dataIndex: 'YG2', key: 'YG2', dispaly: true, filter: false },
  { title: '分配比例2(%)', width: 100, sorter: false, className: "column-money", caption:'ZB2',render: data => renderNumber(data.ZB2), key: 'ZB2', dispaly: true, filter: false },
  { title: '客户经理3', width: 180, sorter: false, dataIndex: 'YG3', key: 'YG3', dispaly: true, filter: false },
  { title: '分配比例3(%)', width: 100, sorter: false, className: "column-money", caption:'ZB3',render: data => renderNumber(data.ZB3), key: 'ZB3', dispaly: true, filter: false },
  { title: '客户经理4', width: 180, sorter: false, dataIndex: 'YG4', key: 'YG4', dispaly: true, filter: false },
  { title: '分配比例4(%)', width: 100, sorter: false, className: "column-money", caption:'ZB4',render: data => renderNumber(data.ZB4), key: 'ZB4', dispaly: true, filter: false },
  { title: '客户经理5', width: 180, sorter: false, dataIndex: 'YG5', key: 'YG5', dispaly: true, filter: false },
  { title: '分配比例5(%)', width: 100, sorter: false, className: "column-money", caption:'ZB5',render: data => renderNumber(data.ZB5), key: 'ZB5', dispaly: true, filter: false },
];
const controls = [
  { name: 'KHMC', required: true, type: 'text', label: '客户名称', grid: 12 / 24 },
  { name: 'ZH', required: true, type: 'text', label: '帐户', grid: 12 / 24 },
  { name: 'KHWD', required: true, type: 'text', label: '开户网点', grid: 12 / 24 },
  { name: 'KHRQ', required: true, type: 'date', label: '开户日期', grid: 12 / 24 },
  { name: 'QXR', required: true, type: 'date', label: '起息日', grid: 12 / 24 },
  { name: 'DQR', required: true, type: 'date', label: '到期日', grid: 12 / 24 },
  { name: 'QYGM', required: true, type: 'text', label: '企业规模', grid: 12 / 24 },
  { name: 'KM', required: true, type: 'text', label: '科目', grid: 12 / 24 },
  { name: 'INRT', required: true, type: 'number', label: '利率', grid: 12 / 24 },
  { name: 'DQYE', required: true, type: 'number', label: '当前余额', grid: 12 / 24 },
  { name: 'BZ', required: true, type: 'text', label: '币种', grid: 12 / 24 },
  { name: 'KHRJ', required: true, type: 'number', label: '开户日均', grid: 12 / 24 },
];
const EditFrom = dataEditCreate(daoName);
const DataTable = dataTableCreate(daoName);
/**对公贷款 */
class COMP_LOAN extends Component {
  constructor(props) {
    super(props);
    this.state = {
      dataChange: false,
      diviList:[],
    };
  }
  componentDidMount() {
    this.props.dispatch({
      type: `${daoName}/setKeyName`,
      payload: { keyName: "ID", permID: this.props.permID }
      
    });
    this.props.dispatch({
      type: `${daoName}/fetch`,
      payload: { daoName, page: 1, pageSize: 20 }
    });
   
    this.loadOptionData();
  }
  onSelectChangeHandler(newList) {
    this.state.diviList = [];
    let diviList = this.state.diviList
    newList.map(data => {
      diviList.push(data)
    })
  }
  loadOptionData() {
    let options = []
    controls.map(c => {
      if (c.fetch) {
        let f = c.fetch;
        if (c.type === 'cascader-select') {
          f.isCascader = true;
        }
        if (c.type !== 'search') {
          options.push(f);
        }
      }
    })
    if (daoName && options.length > 0) {
      this.props.dispatch({ type: `${daoName}/fetchOptions`, payload: { options } });
    }
  }
  findData(){
    const form = this.refs.findForm.getForm();
    let lx='';
    let seeks = [];
    form.validateFields((err, values) => {
      if (!err) {
        lx=values.qygm;
      }
    });
    if(lx=='all'){
      seeks = [];
    }else {
      let whereItems = [];
      let WhereCauses = [];
      whereItems.push({
        FieldName: "QYLX",
        Operator: "=",
        FieldValues:lx
      });
      WhereCauses.push({
        WhereItems: whereItems
      })
      seeks = WhereCauses;
    }
    this.props.dispatch({
      type: `${daoName}/fetch`,
      payload: { daoName, seeks }
    });
  }
  llwh() {//利率维护
    let len = this.props.selectedRowKeys.length;
    let key = this.props.selectedRowKeys;
    const rows = this.state.diviList
    if (len === 0){
      message.info('请选择要维护数据行！');
      return;
    }
    let isOK = true;
    const form = this.refs.whForm.getForm();
    let ll = 0;
    form.validateFields((err, values) => {
      if (!err) {
        if (values.LL === undefined || values.LL === '') {
          message.info('请填入利率值！');
          isOK = false;
        }
        ll = parseFloat(values.LL);
      }
    });
    if (!isOK) {
      return;
    }
    let acid = key[0];
    Modal.confirm({
      title: '提示',
      content: `请确认给【${rows[0].KHMC}】维护利率？`,
      okText: '确定',
      cancelText: '取消',
      onOk: () => {
        compDiviService.UpdateCompAccoInrt({ acid,ll }
        ).then(function (data) {
          if (data === 'OK') {
            message.success('利率维护成功！')
          } else {
            return;
          }
        }).catch(function (ex) {
          console.log('请求出错', ex)
        });
        const form = this.refs.whForm.getForm();
        form.resetFields();
        let sort = {
          field: 'KHRQ',
          order: 'descend'
        }
        this.props.dispatch({
          type: `${daoName}/fetch`,
          payload: { daoName, page: 1, pageSize: 20 ,sorter: sort}
        });              
      }
    });
  }
  render() {
    let { showEdit, record, permID,popedomTypes } = this.props;
    const pTypes = popedomTypes[permID]
    let controlsFilter=[]
    if (pTypes.indexOf('D') !== -1) {
      const controlsFilter1 = [
        { name: 'LL', required: false, type: 'input-number-min0', label: '利率', grid: 3 / 24, layout: { labelSpan: 9, wrapperSpan: 15 } },
        { name: 'CX', required: false, type: 'button-group', data: [{ value: '1', label: '利率维护', onClick: this.llwh.bind(this) }], grid: 5 / 24 },
        
      ];
      controlsFilter=controlsFilter1
    }
    
    return (
      <div className={styles.normal} style={{ background: '#fff', marginTop: -16, padding: '5px 5px' }}>
        <div>
          <div className={styles.create}>
            <EditFrom ref="whForm" daoName={daoName} controls={controlsFilter} clearItemMargin={true} record={{ qygm: 'all' }} selectFirst={false}/>
          </div>
          <DataTable ref="userTable" daoName={daoName} columns={columns} controls={controls} permID={permID} onSelectChange={this.onSelectChangeHandler.bind(this)}/>
        </div>
      </div>
    )
  }
}
function mapStateToProps(state) {
  const { showEdit, record, selectedRowKeys, keyName } = state[daoName];
  const { userID, orgID, pOrgID, popedomTypes } = state.menus;
  return {
    showEdit,
    selectedRowKeys,
    keyName,
    record,
    popedomTypes
  }
}
COMP_LOAN = connect(mapStateToProps)(COMP_LOAN)
export default COMP_LOAN;
let render = (props) => React.createElement(COMP_LOAN, props);
let f = new Factory()
f.set("COMP_LOAN", render)